package org.oexam.ostudy.sl;

import java.sql.SQLException;
import java.util.List;

import javax.naming.NamingException;

import org.oexam.ostudy.db.TestSetsAccess;
import org.oexam.ostudy.facade.struct.InsertNewTestDetails;
import org.oexam.ostudy.facade.struct.InsertNewTestSetDetails;
import org.oexam.ostudy.facade.struct.TestNameAndId;
import org.oexam.ostudy.facade.struct.TestSetName;
import org.oexam.ostudy.facade.struct.TestSetNameList;
import org.oexam.ostudy.struct.TestSets;
import org.oexam.ostudy.struct.Tests;

public class ManageTestSets {

	public String addTestSet(InsertNewTestSetDetails details) throws SQLException, NamingException{
		
		TestSets testSets = new TestSets();
		TestSetsAccess testSetaccess = new TestSetsAccess();
		
		
		testSets.setTestSet_id(testSetaccess.getLastTestSetID()+1);
		testSets.setActive(details.isActive);
		testSets.setNoOfTests(details.noOfTests);
		testSets.setTestSet_name(details.testSet_name.trim());
		
		if(testSetaccess.checkIfTestSetNameExists(testSets.getTestSet_name())){
			return "Given Test name already exists. Please enter a different one.";
		}
			
		testSets.setTestSetMonth(details.testSetMonth);
		testSets.setTestSetYear(details.testSetYear);
		
		testSetaccess.insertTestSet(testSets);		
		
		return "ok";
	}
	
	@SuppressWarnings("deprecation")
	public String addTesttoTestSet(InsertNewTestDetails details) throws SQLException, NamingException{
		
		Tests tests = new Tests();
		TestSetsAccess testSetaccess = new TestSetsAccess();
		
		
		
		if(testSetaccess.checkIfTestNameExists(details.getTest_name())){
			return "Given Test name already exists. Please enter a different one.";
		}
			
	   tests.setTest_id(testSetaccess.getLastTestID()+1);
	   tests.setActive(details.isActive());
	   tests.setNo_of_questions(details.getNo_of_questions());
	   tests.setTeacher(details.getTeacher());
	   tests.setTest_duration(details.getTest_duration());
	   tests.setTest_name(details.getTest_name());
	   tests.setTest_set(details.getTest_set());
	   
	   java.util.Date date = new java.util.Date();
	   date.setDate(details.getTest_day());
	   date.setMonth(details.getTest_month());
	   date.setYear(details.getTest_year());
	   
	   tests.setTest_date(date);
		
		testSetaccess.insertTestToTestSet(tests);		
		
		return "ok";
	}
	
	public TestSetNameList getAllTestSetsName() throws SQLException, NamingException{

		TestSetNameList ts = new TestSetNameList();

		ts.setList(new TestSetsAccess().getAllTestSetNamesAndIDs());
		return ts;
	}
	
	public TestNameAndId getTestNameUsingId(int tID) throws SQLException, NamingException{
		
		Tests test = new TestSetsAccess().getTestDetailsUsingID(tID);
		TestNameAndId nameAndId = new TestNameAndId();
	    nameAndId.setId(test.getTest_id());
	    nameAndId.setTestName(test.getTest_name());
	    
		return nameAndId; 
	}
	
	public TestSetName getTestSetNameUsingId(int tsID) throws SQLException, NamingException{
		
		TestSets testSet = new TestSetsAccess().getTestSetDetailsUsingID(tsID);
		TestSetName nameAndId = new TestSetName();
	    nameAndId.setTestSetID(testSet.getTestSet_id());
	    nameAndId.setTestSetName((testSet.getTestSet_name()));
	    
		return nameAndId; 
	}
	
	public List<TestNameAndId> getTestsNameAndIdForATestSet(int testSetID) throws SQLException, NamingException{
		
		return new TestSetsAccess().getAllTestsNamesAndIDsUsingTestSetID(testSetID);
	}
	
	
}
